<template>
  <div>
    <el-card style=";padding: 0;margin-left: -15px;margin-top:-15px;height: 615px;width: 1100px;">
      <div class="common-layout" style="display: flex;flex-direction: row;">

        <div class="aside" style="padding: 0;margin: 0;background-color: #ffffff;">

          <div style="padding-left:10px">
            <h1>消息盒子</h1>
          </div>

          <div style="margin-top: 80px;margin-left: 10px">
            <span>聊天列表</span>

            <div style="margin-top: 20px">
              <div
                  v-for="(party , index) in store.parties"
                  :key="index"
                  class="infoOfParty"
                  :class="{beChosen:party === isActive}"
              >
                <div @click="jump(party)" style="display: flex;flex-direction: row;justify-content: stretch">
                  <div
                      style="margin: auto;height:80px;display: flex;flex-direction: row;align-items: center">
                    <div class="infoOfParty-img-div" style="margin-left: -110px;">
                      <img class="infoOfParty-img" src="../assets/logo.png" alt="">
                    </div>
                    <span class="infoOfParty-span" style="color: black;display: inline-block;">{{ party }}</span>
                  </div>
                </div>

              </div>
            </div>
          </div>

        </div>

        <div class="right"
             style="display: flex;flex-direction: column;justify-content: stretch;padding: 0;margin: 0;background-color: #ffffff">
          <div class="m-b-right-top" style="width: 100%;height: 64px;">
            <div
                style="margin: auto;height:64px;display: flex;flex-direction: row;align-items: center">
              <div class="infoOfParty-img-div" style="margin-left: 20px;">
                <img class="infoOfParty-img" src="../assets/logo.png" alt="">
              </div>
              <span class="infoOfParty-span" style="color: black;display: inline-block;font-weight: 600;font-size: 20px">{{ isActive }}</span>
            </div>
          </div>
          <main-of-msg-box></main-of-msg-box>
        </div>

      </div>
    </el-card>
  </div>
</template>

<script>
import {useStore} from "../store/msgBox";
import mainOfMsgBox from "@/components/MainOfMsgBox";
import axios from "axios";
import {ref} from 'vue'

export default {
  components: {mainOfMsgBox},
  name: "msgBox",
  setup() {
    const store = useStore()
    let isActive = ref('')

    function jump(party) {
      axios.get('http://120.48.51.66:8081/inform/getMessageListByName', {
        params: {
          userName: store.name,
          otherName: party
        }
      }).then((response) => {
        sessionStorage.setItem("msgRecord", JSON.stringify(response.data.data.informList))
        store.msgRecord = JSON.parse(sessionStorage.getItem("msgRecord"))
      })
          .catch(function (error) {
            console.log(error)
          })
      isActive.value = party
      sessionStorage.setItem("theActive", JSON.stringify(isActive))
      store.sendContent = ''
    }

    return {
      jump,
      store,
      isActive,
    }
  },
  created() {
    if (location.href.indexOf("#reloaded") == -1) {
      location.href = location.href + "#reloaded";
      location.reload();
    }
    // setInterval(()=>{
    //     axios.get("http://120.48.51.66:8081/inform/getSessionByName", {
    //       params: {
    //         userName: this.store.name
    //       }
    //     }).then((response) => {
    //       this.store.parties = response.data.data.sessionList
    //       this.isActive = this.store.parties[0]
    //       axios.get('http://120.48.51.66:8081/inform/getMessageListByName', {
    //         params: {
    //           userName: this.store.name,
    //           otherName: this.isActive
    //         }
    //       }).then((response) => {
    //         this.store.msgRecord = response.data.data.informList
    //         sessionStorage.setItem('msgRecord', JSON.stringify(this.store.msgRecord))
    //       })
    //           .catch(function (error) {
    //             console.log(error)
    //           })
    //     }).catch(function (error) {
    //       console.log(error)
    //     })
    // },1000)
    axios.get("http://120.48.51.66:8081/inform/getSessionByName", {
      params: {
        userName: this.store.name
      }
    }).then((response) => {
      this.store.parties = response.data.data.sessionList
      this.isActive = this.store.parties[0]
      axios.get('http://120.48.51.66:8081/inform/getMessageListByName', {
        params: {
          userName: this.store.name,
          otherName: this.isActive
        }
      }).then((response) => {
        this.store.msgRecord = response.data.data.informList
        sessionStorage.setItem('msgRecord', JSON.stringify(this.store.msgRecord))
      })
          .catch(function (error) {
            console.log(error)
          })
    }).catch(function (error) {
      console.log(error)
    })
  },
}
</script>

<style scoped>

.common-layout {
  /*border: 2px solid ;*/
  margin: 0;
  height: 600px;
  width: 100%;
  /*float: left;*/
  border-radius: 10px;
}

.aside {
  width: 312px;
  margin: 5px 3px;
  height: 590px;
  box-sizing: border-box;
  overflow: hidden scroll;
  border-right: 1px solid #eeeeee;
}

.aside::-webkit-scrollbar {
  width: 8px;
  height: 1px;
}

.aside::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: #9e9e9e;
}

.aside::-webkit-scrollbar-track {
  border-radius: 10px;
  background: #ededed;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, .1);
}

.infoOfParty {
  margin: 25px -12px 0;
  width: 270px;
  height: 80px;
  border-radius: 10px;
  line-height: 40px;
  padding-top: 0;
  background-color: #ffffff;
}

.beChosen {
  /*  box-sizing: border-box;
    background-color: rgb(235, 235, 235);
    border-left: 3px blue solid;*/
  background-color: #1d90f5;
  transition: 0.3s;
  box-shadow: 3px 2px 10px 0px rgba(0, 136, 255);
}

.infoOfParty-span {
  text-align: left;
  display: inline-block;
  font-size: 20px;
  font-weight: normal;
  margin-left: 10px;
}

.infoOfParty-img-div {
  border-radius: 50%;
  border: 2px solid rgb(255, 255, 255);
  width: 50px;
  height: 50px;
  position: relative;
}

.infoOfParty-img-div::before {
  content: '';
  width: 15px;
  height: 15px;
  z-index: 1;
  display: block;
  border-radius: 50%;
  background-color: rgb(144, 225, 80);
  position: absolute;
  right: 0;
}

.infoOfParty-img {
  border-radius: 50%;
  box-sizing: border-box;
  width: 45px;
  height: 45px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  vertical-align: middle;
}

.infoOfParty :hover {
  background-color: #1d90f5;
  transition: 0.3s;
  box-shadow: 0px 0px 10px 0px rgba(0, 136, 255);
  border-radius: 10px;
}

.right {
  margin: 5px;
  width: 77%;
}

</style>
